//
//  DeviceConditionCell.m
//  YSSmartHome
//
//  Created by yanghao on 16/9/13.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "DeviceConditionCell.h"

@interface DeviceConditionCell ()
@property(nonatomic, strong)UILabel *nameLabel;
@property(nonatomic, strong)UIButton *closeSelectedBtn;
@property(nonatomic, strong)UIButton *openSelectedBtn;
@property(nonatomic, strong)UIButton *closeStatuBtn;
@property(nonatomic, strong)UIButton *openStatuBtn;
@end
@implementation DeviceConditionCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(10);
        make.top.equalTo(self.contentView).offset(10);
        make.height.equalTo(30);
    }];
    
    [self.contentView addSubview:self.openSelectedBtn];
    [self.openSelectedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.nameLabel);
        make.top.equalTo(self.nameLabel.bottom).offset(10);
        make.height.equalTo(30);
        make.width.equalTo(30);
    }];
    
    [self.contentView addSubview:self.openStatuBtn];
    [self.openStatuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.openSelectedBtn);
        make.leading.equalTo(self.openSelectedBtn.trailing).offset(20);
        make.trailing.equalTo(self.contentView.centerX);
    }];
    
    [self.contentView addSubview:self.closeSelectedBtn];
    [self.closeSelectedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.centerX).offset(10);
        make.top.equalTo(self.openSelectedBtn);
        make.height.equalTo(30);
        make.width.equalTo(30);
    }];
    
    [self.contentView addSubview:self.closeStatuBtn];
    [self.closeStatuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.closeSelectedBtn);
        make.leading.equalTo(self.closeSelectedBtn.trailing).offset(20);
        make.trailing.equalTo(self.contentView);
    }];
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelect:(ZTSelect *)select
{
    _select = select;
    [self setDevice:select.device];
    self.nameLabel.text = select.name;
    switch (select.slectStatu)
    {
        case SelectStatuOpen:
        {
            self.openSelectedBtn.selected = YES;
            self.closeSelectedBtn.selected = NO;
        }
            break;
        case SelectStatuClose:
        {
            self.openSelectedBtn.selected = NO;
            self.closeSelectedBtn.selected = YES;
        }
            break;
        case SelectStatuUnkonw:
        {
            self.openSelectedBtn.selected = NO;
            self.closeSelectedBtn.selected = NO;
        }
            break;
        default:
            break;
    }
    [_openStatuBtn setTitle:select.strSelectDesc forState:UIControlStateNormal];
    [_closeStatuBtn setTitle:select.strNormalDesc forState:UIControlStateNormal];
}
- (void)setDevice:(ZTDevice *)device
{
    switch (device.devType)
    {
        case ZTDeviceTypeOneWay0Switch:
        case ZTDeviceTypeOneWaySwitch:
        case ZTDeviceTypeTwoWay0Switch:
        case ZTDeviceTypeTwoWaySwitch:
        case ZTDeviceTypeThreeWay0Switch:
        case ZTDeviceTypeThreeWaySwitch:
        case ZTDeviceTypeFourWay0Switch:
        case ZTDeviceTypeFourWaySwitch:
        {
            [_openStatuBtn setImage:[UIImage imageNamed:@"scene_light_open"] forState:UIControlStateNormal];
            [_closeStatuBtn setImage:[UIImage imageNamed:@"scene_light_close"] forState:UIControlStateNormal];
        }
            break;
        case ZTDeviceTypeOneWaySocket:
        {
            [_openStatuBtn setImage:[UIImage imageNamed:@"scene_socket_open"] forState:UIControlStateNormal];
            [_closeStatuBtn setImage:[UIImage imageNamed:@"scene_socket_close"] forState:UIControlStateNormal];
        }
            break;
        case ZTDeviceTypeZTDoorsensor:
        {
            [_openStatuBtn setImage:[UIImage imageNamed:@"scene_door_open"] forState:UIControlStateNormal];
            [_closeStatuBtn setImage:[UIImage imageNamed:@"scene_door_close"] forState:UIControlStateNormal];
        }
            break;
        case ZTDeviceTypeInfraredDetector:
        {
            [_openStatuBtn setImage:[UIImage imageNamed:@"scene_infrareddetector_open"] forState:UIControlStateNormal];
            [_closeStatuBtn setImage:[UIImage imageNamed:@"scene_infrareddetector_close"] forState:UIControlStateNormal];
        }
            break;
        case ZTDeviceTypeSmoke:
        case ZTDeviceTypeGAS:
        {
            [_openStatuBtn setImage:[UIImage imageNamed:@"scene_smoke_gas_open"] forState:UIControlStateNormal];
            [_closeStatuBtn setImage:[UIImage imageNamed:@"scene_smoke_gas_close"] forState:UIControlStateNormal];
        }
            break;
        case ZTDeviceTypeAlarmkey:
        {
            [_openStatuBtn setImage:[UIImage imageNamed:@"scene_alarm_open"] forState:UIControlStateNormal];
            [_closeStatuBtn setImage:[UIImage imageNamed:@"scene_alarm_close"] forState:UIControlStateNormal];
        }
            break;
        case ZTDeviceTypeCurtainSingleTrack:
        case ZTDeviceTypeCurtainDoubleTrack:
        {
            [_openStatuBtn setImage:[UIImage imageNamed:@"scene_curtain_open"] forState:UIControlStateNormal];
            [_closeStatuBtn setImage:[UIImage imageNamed:@"scene_curtain_close"] forState:UIControlStateNormal];
        }
            break;
        case ZTDeviceTypeFlooding:
        {
            [_openStatuBtn setImage:[UIImage imageNamed:@"scene_flood_open"] forState:UIControlStateNormal];
            [_closeStatuBtn setImage:[UIImage imageNamed:@"scene_flood_close"] forState:UIControlStateNormal];
        }
            break;
        default:
            break;
    }
   
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


#pragma mark - action
- (void)slectBtnClick:(UIButton *)btn {
    
    if (btn == self.openSelectedBtn) {
        self.openSelectedBtn.selected = !self.openSelectedBtn.selected;
        self.closeSelectedBtn.selected = NO;
    }else {
        self.closeSelectedBtn.selected = !self.closeSelectedBtn.selected;
        self.openSelectedBtn.selected = NO;
    }
    SelectStatu selectStatu;
    if (self.openSelectedBtn.selected ) {
        selectStatu = SelectStatuOpen;
    } else if (self.closeSelectedBtn.selected)
    {
        selectStatu  =  SelectStatuClose;
    } else{
        selectStatu  =  SelectStatuUnkonw;
    }
    UIView *v = [btn superview];//获取父类view
    UITableViewCell *cell = (UITableViewCell *)[v superview];//获取cell
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"statu"] = @(selectStatu);
    dict[@"cell"] = cell;
    [[NSNotificationCenter defaultCenter]postNotificationName:@"DeviceConditionBtnClickNotif" object:nil userInfo:dict];
    
}

#pragma mark - getters and setters

- (UILabel *)nameLabel
{
    if (!_nameLabel ) {
        _nameLabel = [[UILabel alloc]init];        
    }
    return _nameLabel;
}
- (UIButton *)openSelectedBtn
{
    if (!_openSelectedBtn) {
        _openSelectedBtn = [[UIButton alloc]init];
        [_openSelectedBtn setImage:[UIImage imageNamed:@"scene_condition_notselected"] forState:UIControlStateNormal];
        [_openSelectedBtn setImage:[UIImage imageNamed:@"scene_condition_selected"] forState:UIControlStateSelected];
        [_openSelectedBtn addTarget:self action:@selector(slectBtnClick:) forControlEvents:UIControlEventTouchUpInside];        
    }
    return _openSelectedBtn;
}

- (UIButton *)closeSelectedBtn
{
    if (!_closeSelectedBtn) {
        _closeSelectedBtn = [[UIButton alloc]init];
        [_closeSelectedBtn setImage:[UIImage imageNamed:@"scene_condition_notselected"] forState:UIControlStateNormal];
        [_closeSelectedBtn setImage:[UIImage imageNamed:@"scene_condition_selected"] forState:UIControlStateSelected];
        [_closeSelectedBtn addTarget:self action:@selector(slectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeSelectedBtn;
}
- (UIButton *)closeStatuBtn
{
    if (!_closeStatuBtn) {
        _closeStatuBtn = [[UIButton alloc]init];
//        [_closeStatuBtn setImage:[UIImage imageNamed:@"scene_light_close"] forState:UIControlStateNormal];
//        [_closeStatuBtn setTitle:@"关灯" forState:UIControlStateNormal];
        _closeStatuBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_closeStatuBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _closeStatuBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
         _closeStatuBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        
    }
    return _closeStatuBtn;
}
- (UIButton *)openStatuBtn
{
    if (!_openStatuBtn) {
        _openStatuBtn = [[UIButton alloc]init];
//        [_openStatuBtn setImage:[UIImage imageNamed:@"scene_light_open"] forState:UIControlStateNormal];
//        [_openStatuBtn setTitle:@"开灯" forState:UIControlStateNormal];
        _openStatuBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_openStatuBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _openStatuBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _openStatuBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    }
    return _openStatuBtn;
}
@end
